<?php

/**
 * Add the facebook app url in front.
 *
 */
function fb_url_for($parameters = array(), $absolute = false, $app = null)
{
    return UrlFunctions::addCanvasPrefix(url_for($parameters, false), $absolute, $app);
}


function fb_image_tag($source, $options = array())
{
    $options['absolute'] = true;
    return image_tag($source, $options);
}

function _google_analytics_find_url()
{
    $pathArray = sfContext::getInstance()->getRequest()->getPathInfoArray();
    $url = isset($pathArray['REQUEST_URI']) ? $pathArray['REQUEST_URI'] : '';

    // Add page_id if possible
    try {
        if (strpos('page_id=', $url) === false && sfContext::getInstance()->getUser()->getPageId()) {
            $url .= strpos($url, '?') === false ? '?' : '&';
            $url .= 'page_id='.sfContext::getInstance()->getUser()->getPageId();
        }
    } catch (Exception $e) {
    }

    try {
        $app = App::getCurrentApp();
    } catch (Exception $e) {
        $app = null;
    }
    $url = UrlFunctions::addCanvasPrefix($url, false, $app);

    return $url;
}

function google_analytics_tag($url_params = null)
{
    $domain = sfConfig::get('sf_vtns_base_domain');
    $uacode = sfConfig::get('app_google_analytics');
    $url = _google_analytics_find_url();
    if ($url_params) {
        $url .= strpos($url, '?') === false ? '?' : '&';
        $url .= $url_params;
    }
    return <<<EOF
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(
    ['_setAccount', '{$uacode}'],
    ['_setDomainName', '.work4labs.com'],
    ['_setAllowLinker', true],
    ['_setCampSourceKey', 'source'],
    ['_setCampMediumKey', 'source'],
    ['_setCampNameKey', 'source'],
    ['_setCampContentKey', 'data'],
    ['_trackPageview', '{$url}']
    );
</script>

EOF;
}

function google_adwords_conversions_tag($step)
{
    switch ($step) {
        case 'step0':
            $label = '4SAdCInLtwIQ36vu1QM';
            break;
        case 'step1':
            $label = 'jb_4CIHMtwIQ36vu1QM';
            break;
        case 'step2':
            $label = 'xNSoCPnMtwIQ36vu1QM';
            break;
        case 'step3':
            $label = 'lxuCCPHNtwIQ36vu1QM';
            break;
        case 'step4':
            $label = 'L_LXCOnOtwIQ36vu1QM';
            break;
        case 'step5':
            $label = 'BqOdCOHPtwIQ36vu1QM';
            break;
        default:
            return '';
    }

    $request = sfContext::getInstance()->getRequest();
    $url = 'http'.($request->isSecure() ? 's' : '').'://';
    $url .= 'www.googleadservices.com/pagead/conversion/985372127/?label='.$label.'&amp;guid=ON&amp;script=0';

    if (sfConfig::get('sf_environment') === 'dev')
        return <<<EOF
<div style="display: none;">
AdWords conversion tag: "$url"
</div>
EOF;
    else
        return <<<EOF
<div style="display:inline;">
<img height="1" width="1" style="border-style:none;" alt="" src="$url"/>
</div>
EOF;
}

function tabbed_menu_item($label, $link_parameters, $highlighted_actions)
{
    $request = sfContext::getInstance()->getRequest();
    $options = array();

    if (!is_array($highlighted_actions))
        $highlighted_actions = array($highlighted_actions);
    if (in_array($request->getParameter('action'), $highlighted_actions))
        $options['class'] = 'selected';

    if (App::getCurrentApiId() != App::getMainApiId()) {
        $options['target'] = '_top';
        $link_parameters = fb_url_for($link_parameters, true);
    }

    return link_to($label, $link_parameters, $options);
}

function include_stylesheets_inline()
{
    $response = sfContext::getInstance()->getResponse();
    sfConfig::set('symfony.asset.stylesheets_included', true);

    echo "<style type=\"text/css\">\n";
    foreach ($response->getStylesheets() as $file => $options) {
        // Remove the qery string part added by FacebookResponse
        $pos = strpos($file, '?');
        if ($pos !== false)
            $file = substr($file, 0, $pos);
        include('css/'.$file);
    }
    echo "</style>\n";
}

function include_banner($url, $map = null, $mapname = null, $options = array())
{
    if (!$url || strpos($url, '://') === false)
        return false;

    if ($map) {
        if ($mapname === null)
            $mapname = 'map';
        $options['usemap'] = '#'.$mapname;
        $options['border'] = '0';
    }
    echo image_tag($url, $options);
    if ($map) {
        echo content_tag('map', $map, array('name' => $mapname));
    }
    return true;
}
